package com.ohuang.jiaxiaotong.Activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.myhttp.CallBackString;
import com.example.myhttp.Ihttp;
import com.ohuang.jiaxiaotong.MyHttpURL;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.Util.Sys;
import com.ohuang.jiaxiaotong.base.BaseAc;
import com.ohuang.jiaxiaotong.entity.EventMsg;
import com.ohuang.jiaxiaotong.entity.User;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Objects;

public class CreateClassActivity extends BaseAc {
    private EditText editText_name;
    private Button button_create;
    private String ClassName = "";
    private final static int CREATE_ERROR = 0, JOIN_OK = 1, JOIN_ERROR = 2, NET_ERROR = 3, NET_OTHER = 4;
    private User user;
    private Dialog dialogLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_class);
        EventBus.getDefault().register(this);
        setTitle("创建班级");
        Ohuang.changeStatusBarTextImgColor(this, true);
        Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.mipmap.icon_goback);//设置ActionBar返回按钮
        editText_name = findViewById(R.id.edit_className_createClass);
        button_create = findViewById(R.id.btn_create_CreateClass);

        button_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Sys.isClick()) {
                    if (user != null) {
                        ClassName = editText_name.getText().toString();
                        if (ClassName.isEmpty()) {
                            return;
                        }
                        dialogLoading = Ohuang.dialogLoadingshow(CreateClassActivity.this, "正在创建");

                        HttpCreate();//发送创建班级请求

                    } else {
                        Toast.makeText(CreateClassActivity.this, "获取不到用户,请重启APP再试", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    //创建班级请求
    private void HttpCreate() {
        HashMap<String, String> map = new HashMap<>();
        map.put("className", ClassName);
        Ihttp.getInstance().post(MyHttpURL.mainUrl + "teacher/createClass", map, new CallBackString() {
            @Override
            public void success(String ojb) {
                String code = ojb;
                if ("success".equals(code)) {
                    HttpJoin();//加入班级请求
                } else if ("error".equals(code)) {
                    myHandler.sendEmptyMessage(CREATE_ERROR);
                } else {
                    myHandler.sendEmptyMessage(NET_OTHER);
                }
            }



            @Override
            public void fail(String s) {
                myHandler.sendEmptyMessage(NET_ERROR);
            }
        });

    }

    private void HttpJoin() {
        HashMap<String, String> map = new HashMap<>();
        map.put("className", ClassName);
        map.put("username", user.getUsername());
        map.put("role", "0");
        Ihttp.getInstance().post(MyHttpURL.mainUrl + "user/joinClass", map, new CallBackString() {
            @Override
            public void success(String ojb) {
                String code = ojb;
                if ("success".equals(code)) {
                    myHandler.sendEmptyMessage(JOIN_OK);
                } else if ("error".equals(code)) {
                    myHandler.sendEmptyMessage(JOIN_ERROR);
                } else {
                    myHandler.sendEmptyMessage(NET_OTHER);
                }
            }



            @Override
            public void fail(String s) {
                myHandler.sendEmptyMessage(NET_ERROR);
            }
        });



    }

    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case NET_ERROR:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(CreateClassActivity.this, "创建失败", "网络连接错误,请检查网络");
                    break;
                case NET_OTHER:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(CreateClassActivity.this, "创建失败", "返回值错误");
                    break;
                case JOIN_OK:
                    dialogLoading.dismiss();
                    EventBus.getDefault().post(new EventMsg(1, "加入班级 需要更新班级列表信息"));//需要在ClassFragment和Class2Fragment中处理
                    Ohuang.dialogshow(CreateClassActivity.this, "创建成功", "班级已成功创建。"
                            , new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    finish();
                                }
                            });
                    break;
                case JOIN_ERROR:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(CreateClassActivity.this, "加入失败", "班级加入失败,请联系管理员");
                    break;
                case CREATE_ERROR:
                    dialogLoading.dismiss();
                    Ohuang.dialogshow(CreateClassActivity.this, "创建失败", "该班级名称已被创建,请换一个试试");
                    break;

            }
        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void GetUserEventBus(User user) {
        this.user = user;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {//左上角返回按钮
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
